package com.mall.product.controller;

import com.mall.common.annotation.LogOperation;
import com.mall.common.constant.ProductConstant;
import com.mall.common.utils.Constant;
import com.mall.common.page.PageData;
import com.mall.common.utils.ExcelUtils;
import com.mall.common.utils.Result;
import com.mall.common.validator.AssertUtils;
import com.mall.common.validator.ValidatorUtils;
import com.mall.common.validator.group.AddGroup;
import com.mall.common.validator.group.DefaultGroup;
import com.mall.common.validator.group.UpdateGroup;
import com.mall.product.dto.AttrDTO;
import com.mall.product.dto.ProductAttrValueDTO;
import com.mall.product.excel.AttrExcel;
import com.mall.product.service.AttrService;
import com.mall.product.service.ProductAttrValueService;
import com.mall.product.vo.AttrVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author xjc xjc@163.com
 * @since 1.0.0 2022-07-17
 */
@Slf4j
@RestController
@RequestMapping("product/attr")
@Api(tags="商品属性")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Resource
    private ProductAttrValueService productAttrValueService;
    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataTypeClass = Integer.class) ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataTypeClass = Integer.class) ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataTypeClass = String.class) ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataTypeClass = String.class)
    })
    public Result<PageData<AttrDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<AttrDTO> page = attrService.page(params);

        return new Result<PageData<AttrDTO>>().ok(page);
    }

    @GetMapping("/page/list/{catelogId}")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataTypeClass = Integer.class) ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataTypeClass = Integer.class) ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataTypeClass = String.class) ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataTypeClass = String.class)
    })
    public Result<PageData<AttrVo>> pageByParam(@ApiIgnore @RequestParam Map<String, Object> params,@PathVariable("catelogId")Long catelogId){

        log.info(params.toString());
        PageData<AttrVo> page = attrService.pageByParam(params,catelogId);

        return new Result<PageData<AttrVo>>().ok(page);
    }

    @GetMapping("/list/spu/{spuId}")
    public Result listInfoSpu(@PathVariable("spuId")Long spuId){
        List<ProductAttrValueDTO> productAttrValueDTOS = productAttrValueService.queryBySpuId(spuId);
        return new Result().ok(productAttrValueDTOS);
    }



    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<AttrVo> get(@PathVariable("id") Long id){
        AttrVo attrVo = attrService.getAllDataInfo(id);
        return new Result<AttrVo>().ok(attrVo);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody AttrDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        attrService.saveAttrDto(dto);

        return new Result();
    }


    @GetMapping("/sale/list/{catelogId}")
    @ApiOperation("获取所有的销售属性")
    public Result<List<AttrDTO>> getSaleAttr(@PathVariable("catelogId")Long catelogId){
        List<AttrDTO> res = attrService.ListAttrByCatelogIdAndAttrType(catelogId, ProductConstant.AttrEnum.ATTR_TYPE_SALE.getType());
        return new Result<List<AttrDTO>>().ok(res);
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody AttrDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        attrService.updateAllInfo(dto);
        return new Result();
    }

    @PutMapping("/update/{spuId}")
    @ApiOperation("修改")
    @LogOperation("修改")
    @Transactional
    public Result batchUpdateAttr(@RequestBody List<ProductAttrValueDTO> dto, @PathVariable Long spuId){
        //效验数据
//        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        productAttrValueService.batchUpdateAttr(dto,spuId);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        attrService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<AttrDTO> list = attrService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, AttrExcel.class);
    }

}